<?php
/**
 * Created by PhpStorm.
 * User: JHR
 * Date: 2018/9/25
 * Time: 16:12
 */
namespace app\manage\controller;
use my\MyRedis;
use OSS\Core\OssException;
use OSS\OssClient;
use think\Db;
use think\Controller;
use think\exception\HttpResponseException;
class Base extends Controller {

    protected $cmd;
    protected $controller;
    protected $userinfo;

    private $accessKeyId;
    private $accessKeySecret;
    private $endpoint;
    private $bucket;
    private $arn;

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->controller = request()->controller();
        $this->cmd = request()->controller() . '/' . request()->action();
        $this->accessKeyId = config('accessKeyId');
        $this->accessKeySecret = config('accessKeySecret');
        $this->endpoint = "http://oss-cn-beijing.aliyuncs.com";
        // 存储空间名称
        $this->bucket = config('ali_bucket');
        $this->arn = config('arn');
        $this->needLogin();

    }

    private function needLogin() {
        //无视$need, 直接放行
        $allow = [];
        //需要登录的 controller | action
        $need = [
            'Index','Food','Shop','Home','Admin','User','Alioss','Upload','Article','Chat'
        ];
        $noauth = [
            'chat/historymessage',
            'admin/userinfo',
            'index/getmymenu',
            'admin/adminlist',
            'index/getmymenu',
        ];
        if(in_array($this->cmd,$need) || in_array($this->controller,$need)) {
            if(in_array($this->cmd,$allow)) {
                return true;
            }
            $token = input('post.token');
            if(!$token) {
                throw new HttpResponseException(ajax('请传入token',-6));
            }else {
                $whereToken = [
                    ['token','=',$token]
                ];
                $token_exist = Db::table('mp_admin_token')->where($whereToken)->find();
                if(!$token_exist) {
                    throw new HttpResponseException(ajax('token无效',-3));
                }else {
                    if((time() - $token_exist['create_time']) > config('token_expires_time')) {
                        throw new HttpResponseException(ajax('token已过期',-3));
                    }
                    $this->userinfo = $token_exist;
                    if(config('auth_on') === true) {
                        if($token_exist['admin_id'] === config('superid')) {
                            return true;
                        }else {
                            if(in_array(strtolower($this->cmd),$noauth)) {
                                return true;
                            }
                            $redis = MyRedis::getInstance();
                            $roleAuth = $redis::get('role'.$token_exist['role']);
                            $roleAuth = json_decode($roleAuth, true);
                            if(empty($roleAuth)) {
                                throw new HttpResponseException(ajax('没有权限1',54));
                            }
                            if(!in_array(strtolower($this->cmd),$roleAuth['rules'])) {
                                throw new HttpResponseException(ajax('没有权限2',54));
                            }
                            return true;
                        }
                    }
                }
            }
            return true;
        }else {
            return true;
        }
    }


    public function moveObject($from_obj,$to_path) {
        $arr = explode('/',$from_obj);
        $to_obj = $to_path . end($arr);
        try {
            //如果一样不需要挪动
            if($from_obj == $to_obj) {
                return [
                    'code' => 0,
                    'path' => $to_obj
                ];
            }else {
                $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
                $ossClient->copyObject($this->bucket,$from_obj,$this->bucket,$to_obj);
            }
        } catch (OssException $e) {
            mylog($this->cmd, $e->getMessage(),'alioss.log');
            return [
                'code' => 1,
                'msg' => $e->getMessage()
            ];
        }
        $this->deleteObject($from_obj);
        return [
            'code' => 0,
            'path' => $to_obj
        ];

    }
    //return true false
    public function existObject($obj) {
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $exist = $ossClient->doesObjectExist($this->bucket,$obj);
        } catch (OssException $e) {
            mylog($this->cmd, $e->getMessage(),'alioss.log');
        }
        return $exist;
    }

    public function deleteObject($obj) {
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $info = $ossClient->deleteObject($this->bucket,$obj);
        } catch (OssException $e) {
            mylog($this->cmd, $e->getMessage(),'alioss.log');
            $info = $e->getMessage();
        }
        return $info;
    }

    public function deleteObjects($objs) {
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $info = $ossClient->deleteObjects($this->bucket,$objs);
        } catch (OssException $e) {
            mylog($this->cmd, $e->getMessage(),'alioss.log');
            $info = $e->getMessage();
        }
        return $info;
    }

    public function editorRemovePrefix($content) {
        $editor_prefix = config('editor_prefix');
        $res = str_replace($editor_prefix,'',$content);
//        $res = str_replace('src="/wangeditor','src="wangeditor',$res);
        return $res;
    }

    public function editorAddPrefix($content) {
        $editor_prefix = config('editor_prefix');
        $res = str_replace('src="wangeditor','src="'.$editor_prefix.'wangeditor',$content);
        return $res;
    }






}